<?php


class UserAnswerTable extends myDoctrineTable
{
    
    public static function getInstance()
    {
        return Doctrine_Core::getTable('UserAnswer');
    }
    /**
     * save to database
     * Enter description here ...
     * @param unknown_type $userId
     * @param unknown_type $answerId
     */
    public function saveToUserAnswer($userId,$answerId)
    {
        $userAnswer = new UserAnswer();
        $userAnswer->set("user_id",$userId);
        $userAnswer->set("answer_id",$answerId);
        $userAnswer->set("is_active",1);
        $userAnswer->save();    
    }
	public function saveResponse($userId,$answerId,$questionId,$response)
    {
        $userAnswer = new UserAnswer();
        $userAnswer->set("user_id",$userId);
        $userAnswer->set("answer_id",$answerId);
        $userAnswer->set("question_id",$questionId);
        $userAnswer->set("is_active",1);
        $userAnswer->set("response",$response);
        $userAnswer->save();
    }
    /*
     * query useranswer table,get total of answers with surveyId and questionId
     */
    public function getTotalAnswers($surveyId,$questionId)
    {
    	$totalAnswers = $this->findAll(array("survey_id"=>$surveyId,"question_id"=>$questionId))->count();
    	return $totalAnswers;
    }
    /*
     * get total of a defined answer
     */
    public function getNumRecordAnswer($answerId)
    {
    	$totalAnswers = $this->findByAnswerId($answerId)->count();
    	return $totalAnswers;
    }
    /*
     * return percent of answer 
     */
    public function getPercentAnswer($aAnswer,$total)
    {
    	if($total!=0)
    	return ($aAnswer/$total)*100;
    	else return -1;
    }
    /*
     * count total of response at a question
     */
    public function getNumAnsPerQues($questionId)
    {
    	$numOfRecord = 0;
    	$listAnswerId = Doctrine_Core::getTable('Answer')->findByQuestionId($questionId);
    	foreach ($listAnswerId as $answer)
    	{
    		$answerId = $answer->getId();
    		$numOfRecord += $this->getNumRecordAnswer($answerId);
    	}
    	return $numOfRecord;
    }
}